package com.shareach.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.mongodb.BasicDBObjectBuilder;
import com.shareach.common.BaseQueryOrder;
import com.shareach.common.BizCondition;
import com.shareach.dao.mongo.BaseShopDao;
import com.shareach.dao.redis.impl.MatchRDao;
import com.shareach.domain.BaseShopDomain;
import com.shareach.domain.DiscountType;
import com.shareach.domain.common.Sites;
import com.shareach.service.BaseShopService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.Globals;

@Service
public class BaseShopServiceImpl extends BaseService<BaseShopDomain> implements BaseShopService {

	@Autowired
	private BaseShopDao baseShopDao;
	@Autowired
	private MatchRDao matchRDao ;

	@Override
	public synchronized Long save(BaseShopDomain domain) {
		domain.setCreatTime(new Date());
		return baseShopDao.insert(domain);
	}

	@Override
	public void del(BaseShopDomain domain) {
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(domain.getId()));
		baseShopDao.delete(query, BaseShopDomain.class);
	}

	/**
	 * 必须有id
	 */
	@Override
	public synchronized void update(BaseShopDomain domain) {
		if (domain.getId() == null)
			return;
		Update update = parseDomaintoUpdate(domain);

		String[] tels = domain.getTels();
		if (tels != null && tels.length > 0) {
			update.addToSet("tels", BasicDBObjectBuilder.start("$each", tels).get());
		}
		String[] tags = domain.getTags();
		if (tags != null && tags.length > 0) {
			update.addToSet("tags", BasicDBObjectBuilder.start("$each", tags).get());
		}
		Long[] subCategorys = domain.getSubCategory();
		if (subCategorys != null && subCategorys.length > 0) {
			update.addToSet("subCategory", BasicDBObjectBuilder.start("$each", subCategorys).get());
		}
		Double loc[] = domain.getLoc();
		if (loc != null && loc.length > 0) {
			update.set("loc", loc);
		}
		String[] alias = domain.getAlias();
		if (alias != null && alias.length > 0) {
			update.set("alias", alias);
		}

		Long[] zooms = domain.getZoom();
		if (zooms != null && zooms.length > 0) {
			update.set("zoom", zooms);
		}
		String[] weiboIds = domain.getWeiboIds();
		if (weiboIds != null && weiboIds.length > 0) {
			update.set("weiboIds", weiboIds);
		}

		if (update.getUpdateObject().toMap().size() > 0) {
			baseShopDao.update(Criteria.where("_id").is(domain.getId()), update);
		}
	}

	@Override
	public BaseShopDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BaseShopDomain baseShopDomain = baseShopDao.findOne(query);

		return baseShopDomain;
	}

	@Override
	public List<BaseShopDomain> getList(BaseShopDomain domain) {
		Query query = parseDomaintoQuery(domain);

		List<BaseShopDomain> baseShopDomains = baseShopDao.findList(query);
		return baseShopDomains;
	}

	@Override
	public synchronized Map<String, Object> saveOrUpdate(BaseShopDomain domain) {
		Map<String, Object> retMap = new HashMap<String, Object>();

		try {
			Sites sites = domain.getSites().iterator().next();
			String site = sites.getSite();
			String siteId = sites.getSiteId();

			List<BaseShopDomain> shopList = getList(new BizCondition().put("sites.site", site).put("sites.siteId",
					siteId));

			if (shopList == null || shopList.size() == 0) {
				long idN = save(domain);
				if (0 == idN) {
					throw new Exception("Increase shop id error, the return id is: 0");
				}
				retMap.put(Globals.GLOBAL_SERVICE_RET_ID, domain.getId());
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_NOTEXIST);

				//放入微博采集队列
				matchRDao.setDiscountShopUnmap(DiscountType.Weibo, idN);
				return retMap;
			} else {
				BaseShopDomain shopdomain = shopList.get(0);
				domain.setId(shopdomain.getId());
				//放入微博采集队列
				matchRDao.setDiscountShopUnmap(DiscountType.Weibo, shopdomain.getId());
				update(domain);
				retMap.put(Globals.GLOBAL_SERVICE_RET_ID, shopdomain.getId());
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_EXIST);
				return retMap;
			}
		} catch (Exception e) {
			retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_ERROR);
			e.printStackTrace();
			return retMap;
		}

	}

	@Override
	public void update(BaseShopDomain domain, BizCondition condition) {

		Query query = parseConditionToQuery(condition);

		Update update = parseDomaintoUpdate(domain);
		if (update.getUpdateObject().toMap().size() != 0) {
			baseShopDao.update(query, update);
		}
	}

	@Override
	public List<BaseShopDomain> getList(BizCondition condition) {
		Query query = parseConditionToQuery(condition);
		List<BaseShopDomain> domainList = baseShopDao.findList(query);
		return domainList;
	}

	@Override
	public Page<BaseShopDomain> getPageResult(BaseShopDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BaseShopDomain> domainPage = baseShopDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public void update(BaseShopDomain domain, BaseShopDomain condition) {
		Query query = parseDomaintoQuery(condition);

		Update update = parseDomaintoUpdate(domain);
		if (update.getUpdateObject().toMap().size() != 0) {
			baseShopDao.update(query, update);
		}

	}

	@Override
	public List<BaseShopDomain> getListByIds(List<Long> ids, BaseQueryOrder order) {
		if (null != ids && ids.size() > 0) {
			Query qry = new Query();
			qry.addCriteria(Criteria.where("_id").in(ids));
			return baseShopDao.getPageResult(qry, order).getContent();
		}
		return Lists.newArrayList();
	}

}
